package com.naodong.shenluntiku.mvp.view.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naodong.shenluntiku.R;

/**
 * Created by hujie on 17/6/14.
 * <p>
 * 错误界面
 */

public class ErrorView extends LinearLayout {


    private View emptyView;
    private View netErrorView;


    public ErrorView(Context context) {
        super(context);
        init();
    }

    public ErrorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ErrorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        setGravity(Gravity.CENTER);
        setOrientation(VERTICAL);
        View errorView = LayoutInflater.from(getContext()).inflate(R.layout.error_layout, null);
        addView(errorView);
    }

    public void showEmptyView(String tipText) {
        if (emptyView == null) {
            emptyView = ((ViewStub) findViewById(R.id.emptyView)).inflate();
        }
        if (netErrorView != null) {
            netErrorView.setVisibility(View.GONE);
        }
        emptyView.setVisibility(View.VISIBLE);
        TextView tv = (TextView) emptyView.findViewById(R.id.errorTagText);
        ImageView iv = (ImageView) emptyView.findViewById(R.id.errorImage);
        iv.setImageResource(R.drawable.img_error_tag);
        tv.setText(TextUtils.isEmpty(tipText) ? "暂无数据" : tipText);
    }

    public void showNetErrorView() {
        if (netErrorView == null) {
            netErrorView = ((ViewStub) findViewById(R.id.noNetView)).inflate();
        }
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        netErrorView.setVisibility(View.VISIBLE);
    }

    public void hideAllView() {
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        if (netErrorView != null) {
            netErrorView.setVisibility(View.GONE);
        }
    }

}
